import * as Cesium from 'cesium';
import { ESSceneObject } from 'xbsj-xe2/dist-node/xe2-base-objects';
import { Event, reactArray } from 'xbsj-xe2/dist-node/xe2-base-utils';
import { CzmObject } from 'xbsj-xe2/dist-node/xe2-cesium-objects';
import { UeESSceneObject } from 'xbsj-xe2/dist-node/xe2-ue-objects';

export class ESTestObj extends ESSceneObject {
    // 注册
    static type = this.register('ESTestObj', this, { chsName: 'ESTestObj', tags: ['ESObjects', '_ES_Impl_Cesium'], description: 'ESTestObj' });
    get typeName() { return 'ESTestObj'; }

    // json获取内部数据
    get json() { return this._innerGetJson() }
    set json(value) { this._innerSetJson(value); }


    // 自定义响应式变量
    _arr = this.disposeVar(reactArray([]));
    get arr() { return this._arr.value; }
    set arr(value) { this._arr.value = value }
    get arrChanged() { return this._arr.changed; }


    // 自定义定义方法（需要事件协助触发）
    _testEvent = this.disposeVar(new Event());
    test() { this._testEvent.emit(); }

    constructor(id) {
        super(id);

    }
}

export class CzmESTestObj extends CzmObject {
    static type = this.register(ESTestObj.type, this);
    constructor(sceneObject, czmViewer) {
        super(sceneObject, czmViewer);
        console.log("构造函数CESIUM")
        const viewer = czmViewer.viewer;
        if (!viewer) {
            console.warn(`viewer is undefined!`);
            return;
        }
        // 测试Cesium是否可用
        this.dispose(
            sceneObject._testEvent.don((e) => {
                console.log("cesium")
            })
        )

    }
}

export class UeESTestObj extends UeESSceneObject {
    static type = this.register(ESTestObj.type, this);
    constructor(sceneObject, ueViewer) {
        super(sceneObject, ueViewer);
        console.log("构造函数UE")

        const viewer = ueViewer.viewer;
        if (!viewer) {
            console.warn(`viewer is undefined!`);
            return;
        }

        this.dispose(
            sceneObject._testEvent.don(async (e) => {
                const res = await viewer.callUeFunc({
                    f: 'Test',
                    p: {
                        name: "测试",
                        id: sceneObject.id,
                    }
                });
                console.log(res)
            })
        )

        this.dispose(
            sceneObject.arrChanged.don(async () => {
                const res = await viewer.callUeFunc({
                    f: 'update',
                    p: {
                        id: sceneObject.id,
                        arr: sceneObject.arr,
                    }
                })
                console.log(res)
            })

        )


    }
}


//使用
/**
 * 1.先引入js文件
 * 2.使用
 * 3.ESSceneObject是最基础的类,也可继承其他类，实现类同理
 */
// import "./Test";
// const obj = objm.createSceneObject('ESTestObj');

